<?php

/*#################################################
#
# Copyright (c) 2008 - 2010 ABSoft Company
# Written by Immanuel192
#
# This file is part of ABSoft Framework
*
* Change logs
* 09 - 30 - 2010        immanuel192         First create
*                                           Based on Jquery Color picker
*      http://designreviver.com/wp-content/uploads/2009/01/example.html
* 
* 
##################################################*/

if (!defined('EXPONENT')) exit('');

/**
 * Color picker control
 *
 * @author Dang Tin Trung
 * @copyright 2008 - 2010 ABSoft Company
 * @version 1.0
 * 
 * @package Subsystems
 * @subpackage Forms
 */

/**
 * Manually include the class file for formcontrol, for PHP4
 * (This does not adversely affect PHP5)
 */
require_once(BASE."subsystems/forms/controls/formcontrol.php");

/**
 * Color picker control
 *
 * @package Subsystems
 * @subpackage Forms
 */
class colorpicker extends formcontrol {
	function name() { return "Color picker"; }
	function isSimpleControl() { return true; }
	function getFieldDefinition() {
                return array(
			DB_FIELD_TYPE=>DB_DEF_STRING,
			DB_FIELD_LEN=>250,
		);
        }
	
	function colorpicker( $default = "000000", $load_js = true) {
        $this->load_js = $load_js;
        $this->default=$default;
	}
	
	function onRegister(&$form) {
		//$form->enctype = ""; // no need, just normal
	}

	function controlToHTML($name) {
        $html="";
		if ($this->load_js)
		{
            // js and css loader
            $html = "<script type='text/javascript' src='" .   THEME_RELATIVE . "modules/jcolorpicker/jq.color.js'></script><link rel='stylesheet' href='" . THEME_RELATIVE . "modules/jcolorpicker/ui.colorpicker.css' type='text/css' />";
        }
        // build-in text control to store value
		$html.="
            <div class='colorSelector' id='colorSelector_{$name}'><div style='background-color: #{$this->default};'></div></div>
        <input type='hidden' id='{$name}_colorSelector' name='{$name}' value='{$this->default}'>"; 
        // init script
        $html.="
            <script language='javascript'>
            $(document).ready(function() {
                $('#colorSelector_{$name}').ColorPicker({
                    color: '#{$this->default}',
                    onShow: function (colpkr) {
                        $(colpkr).fadeIn(500);
                        return false;
                    },
                    onHide: function (colpkr) {
                        $(colpkr).fadeOut(500);
                        return false;
                    },
                    onChange: function (hsb, hex, rgb) {
                        $('#colorSelector_{$name} div').css('backgroundColor', '#' + hex);
                        $('#{$name}_colorSelector').val(hex);
                    }
                });
            });
            </script>    
        ";       
		return $html;
	}

	function form($object) {
                if (!defined("SYS_FORMS")) require_once(BASE."subsystems/forms.php");
                exponent_forms_initialize();

                $form = new form();
                if (!isset($object->identifier)) {
                        $object->identifier = "";
                        $object->caption = "";
                        $object->default = "";
                }
                $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');

                $form->register("identifier",$i18n['identifier'],new textcontrol($object->identifier));
                $form->register("caption",$i18n['caption'], new textcontrol($object->caption));
                $form->register("default",$i18n['default'], new textcontrol($object->default));
                $form->register("submit","",new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));
                return $form;
        }

	function update($values, $object) {
                if ($object == null) $object = new uploadcontrol();
                if ($values['identifier'] == "") {
                        $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');
                        $post = $_POST;
                        $post['_formError'] = $i18n['id_req'];
                        exponent_sessions_set("last_POST",$post);
                        return null;
                }
                $object->identifier = $values['identifier'];
                $object->caption = $values['caption'];
                $object->default = $values['default'];
                return $object;
        }

	function parseData($original_name,$formvalues) {
		$file = $formvalues[$original_name];
		return '<a href="'.URL_FULL.$file.'">'.basename($file).'</a>';
	}
}

?>
